//
//  LXModifyPasswordController.m
//  QuickCure1
//
//  Created by Oopsjesse on 31/10/2016.
//  Copyright © 2016 maoge. All rights reserved.
//

#import "LXModifyPasswordController.h"
#import "Users.h"

@interface LXModifyPasswordController ()<UITextFieldDelegate>

/**
 原来的密码
 */
@property (weak, nonatomic) IBOutlet UITextField *originalPassword;

/**
 第一次输入新密码
 */
@property (weak, nonatomic) IBOutlet UITextField *inputNewpassword;

/**
 确认新密码
 */
@property (weak, nonatomic) IBOutlet UITextField *confirmNewsPassword;

@property (nonatomic, weak) UIView *coverView;

@property (nonatomic, weak) UIView *whiteView;


/**
 x修改成功
 */
@property (nonatomic, assign) BOOL isModifySucceed;

/**
 原密码输入错误
 */
@property (nonatomic, assign) BOOL isPasswordError;

/**
 输入一致
 */
@property (nonatomic, assign) BOOL isInputConsistent;


@end

@implementation LXModifyPasswordController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.originalPassword.delegate = self;
    self.inputNewpassword.delegate = self;
    self.confirmNewsPassword.delegate = self;
    
    
}
- (IBAction)clickSubmit:(UIButton *)sender {
    
    NSString *originalPasswordMD5 = [self.originalPassword.text cz_md5String];
    
    if (![originalPasswordMD5 isEqualToString:_user.easymob_password])
    {
        _isPasswordError = YES;
        
        return;
    }
    if ([self.inputNewpassword.text isEqualToString:self.confirmNewsPassword.text])
    {
        _user.easymob_password = [self.inputNewpassword.text cz_md5String];
        
        NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:kUsersInfoPath];
        
        NSMutableArray *arrayM = [NSMutableArray arrayWithArray:array];
        
        [array enumerateObjectsUsingBlock:^(Users * obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj.account isEqualToString:_user.account])
            {
                [arrayM removeObject:obj];
                [arrayM addObject:_user];
                return ;
            }
            
        }];
        
        array = arrayM.copy;
        
        _isModifySucceed = YES;
        
        [NSKeyedArchiver archiveRootObject:array toFile:kUsersInfoPath];
        
        [self displayPromptBox];
    }
    else
    {
        _isInputConsistent = YES;
    }
    
}

#pragma mark -
#pragma mark - 提示框
- (void)displayPromptBox
{
    if (self.coverView == nil)
    {
        UIView *coverView = [[UIView alloc] init];
        
        self.coverView = coverView;
        
        coverView.backgroundColor = [UIColor blackColor];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
        
        [coverView addGestureRecognizer:tap];
        
        coverView.alpha = 0;
        
        UIView *whiteView = [[UIView alloc] init];
        
        whiteView.layer.cornerRadius = 10;
        
        self.whiteView = whiteView;
        
        whiteView.backgroundColor = [UIColor whiteColor];
        
        whiteView.alpha = 0;
        
        [self.view addSubview:coverView];
        [self.view addSubview:whiteView];
        
        UILabel *reminderLabel = [UILabel labelWithTitle:@"温馨提示" andColor:[UIColor blackColor] andFontSize:16];
        reminderLabel.font = [UIFont boldSystemFontOfSize:16];
        
        NSString *str = @"密码修改成功";
        
        if (_isPasswordError)
        {
            str = @"密码输入错误";
        }
        else if (_isInputConsistent)
        {
            str = @"两次密码输入不一致";
        }
        UILabel *messageLabel = [UILabel labelWithTitle:str andColor:[UIColor blackColor] andFontSize:14];
        
        [whiteView addSubview:reminderLabel];
        [whiteView addSubview:messageLabel];
        
        [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.edges.equalTo(self.view);
        }];
        
        [whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.width.mas_equalTo(SCREEN_SIZE.width * 0.8);
            
            make.height.mas_equalTo(60);
            
            make.center.equalTo(self.view);
        }];
        
        [reminderLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.centerX.equalTo(whiteView);
            make.bottom.equalTo(whiteView.mas_centerY).offset(-2);
            
        }];
        
        [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(whiteView);
            make.top.equalTo(whiteView.mas_centerY).offset(2);
        }];
        
        
    }
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.coverView.alpha = 0.5;
        self.whiteView.alpha = 1;
    }];

}

- (void)tap:(UITapGestureRecognizer *)tap
{
    
    
    [UIView animateWithDuration:0.3 animations:^{
        self.coverView.alpha = 0;
        self.whiteView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.coverView removeFromSuperview];
        [self.whiteView removeFromSuperview];
    }];
    if (_isModifySucceed)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    
    _isPasswordError = NO;
    _isInputConsistent = NO;
    _isModifySucceed = NO;
}




@end
